home *** CD-ROM | disk | FTP | other *** search
/ PC-X 1997 October / pcx14_9710.iso / swag / parsing.swg / 0013_Copying a linked list.pas < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-08-30  |  3.2 KB  |  110 lines

  1.  
  2. Program Copy_Linked_List;
  3. { <clifpenn@airmail.net>  May 31, 1996
  4.   Turbo v.6.0 but no TPU's used so should work in many versions.
  5.   For test purposes, a test string is parsed into words and each
  6.   word and its length is stored in the cell of a linked list of
  7.   the queue type (first-in-first-out, FIFO). The first linked list
  8.   is preserved while its data is copied to another linked list.
  9.   As a check on correct execution, the data of both lists are
  10.   displayed on screen side by side.  }
  11.  
  12. CONST
  13.      TestStr = 'The quick brown fox jumped over the lazy dogs back';
  14.  
  15. TYPE
  16.     link = ^cell;
  17.     cell =
  18.          Record
  19.                Next:link;
  20.                StrWord:String[6];
  21.                WordLen:Integer;
  22.          End;
  23. VAR
  24.     head1, head2:link;
  25.  
  26. Procedure String_To_List(VAR head:link);
  27. VAR   Wrd:Array[1..10] of String[6];
  28.       s:String[80];
  29.       p1, tail1:link;
  30.       n, p:Integer;
  31. Begin
  32. (* make an array of words for test purposes *)
  33.       s := TestStr + ' '; (* trailing space makes it easier to parse *)
  34.       n := 0;
  35.       While Length(s) <> 0 do
  36.       Begin
  37.            p := Pos(' ', s);   (* position of first space *)
  38.            Inc(n);
  39.            Wrd[n] := Copy(s, 1, p - 1); (* copies chars less space *)
  40.            Delete(s, 1, p)  (* deletes trailing space, too *);
  41.       End;
  42.       (* n now contains the number of elements in the array *)
  43.  
  44. (* make linked list and transfer array data to cells *);
  45.       p := 0;
  46.       p1 := nil;
  47.       While p < n do
  48.       Begin
  49.            Inc(p);
  50.            New(tail1);
  51.            With tail1^ do
  52.            Begin
  53.                 next := nil;
  54.                 StrWord := Wrd[p];
  55.                 WordLen := Length(Wrd[p]);
  56.            End;
  57.            (* change global variable head1 to point to first cell *)
  58.            If p1 = nil then head := tail1;
  59.            p1^.next := tail1;  (* p1 is still nil for first cell *)
  60.            p1 := tail1; (* p1 to hold old link after a new tail *)
  61.       End;
  62. End;
  63.  
  64. Procedure Copy_List_To_List(hd1:link; VAR hd2:link);
  65. VAR  prev1, prev2, next2:link;
  66. Begin
  67.      prev1 := hd1;    prev2 := nil;
  68.      While prev1 <> nil do
  69.      Begin
  70.          New(next2);
  71.          With next2^ do   (* copy data *)
  72.          Begin
  73.               next := nil; (* not a copy *)
  74.               StrWord := prev1^.StrWord;
  75.               WordLen := prev1^.WordLen;
  76.          End;
  77.          If prev2 = nil then hd2 := next2; (* assigns global head2 *)
  78.          prev2^.next := next2;
  79.          prev2 := next2;
  80.  
  81.          (* advance to new cell of original list. Terminate
  82.             copying if prev1^.next = nil *)
  83.  
  84.          prev1 := prev1^.next;
  85.      End; (* while *)
  86. End;
  87.  
  88. Procedure Show_List_Comparison(h1, h2:link);
  89. Begin
  90.      Writeln; Writeln; Writeln;
  91.      Writeln('Original':15, 'Copy':15);
  92.      Writeln;
  93.      While h1 <> nil do
  94.      Begin
  95.            Write(  h1^.StrWord:10, h1^.WordLen:5, '     ');
  96.            Writeln(h2^.StrWord:10, h2^.WordLen:5);
  97.            h1 := h1^.next;
  98.            h2 := h2^.next;
  99.      End;
  100.      Writeln; Writeln; Writeln('<< Press Enter >>');
  101.      Readln;
  102. End;
  103.  
  104.  
  105. BEGIN { main program -- Copy_Linked_List }
  106.       String_To_List(head1);
  107.       Copy_List_To_List(head1, head2);
  108.       Show_List_Comparison(head1, head2);
  109. END.
  110.